package com.debao;

import com.debao.common.annotation.Anonymous;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.CourseCard;
import com.debao.system.domain.TimeUserCard;
import com.debao.system.domain.UserCourseCard;
import com.debao.system.domain.rsp.SearChRsp;
import com.debao.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/course")
public class ApiCourse {


    @Autowired
    private ICourseCardService courseCardService;

    @Autowired
    private ICourseTypeService courseTypeService;

    @Autowired
    private ITimeUserCardService timeUserCardService;

    @Autowired
    private IUserCourseCardService userCourseCardService;

    /**
     * 获取自己发布的课程卡列表
     */
    @GetMapping("/list/{coachId}")
    public AjaxResult list(@PathVariable("coachId") Long coachId) {
        List<CourseCard> courseCards = courseCardService.selectCourseCardList(new CourseCard().setCoachId(coachId).setOnline(1L));
        return AjaxResult.success(courseCards);
    }
    /**
     * 教练发布课程
     */
    @PostMapping("/pushCourse")
    public AjaxResult pushCourse(@Validated @RequestBody CourseCard courseCard) {
        courseCardService.insertCourseCard(courseCard);
        return AjaxResult.success();
    }
    /**
     * 课程上下线
     */
    @PostMapping("/isOnline")
    public AjaxResult isOnline(@RequestParam Long id, @RequestParam Long online) {
        int row = courseCardService.updateCourseCard(new CourseCard().setId(id).setOnline(online));
        if (row==1){
            return AjaxResult.success("课程状态修改成功");
        }
        return AjaxResult.error("课程状态修改失败");
    }
    /**
     * 获取课程详情
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        CourseCard courseCard = courseCardService.selectCourseCardById(id);
        return AjaxResult.success(courseCard);
    }
    /**
     * 修改课程卡
     */
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody CourseCard courseCard) {
        return AjaxResult.success(courseCardService.updateCourseCard(courseCard));
    }
    /**
     * 获取课程类型
     */
    @GetMapping("/type")
    public AjaxResult type() {
        return AjaxResult.success(courseTypeService.selectCourseTypeList(null));
    }
    /**
     * 获取用户购买的课程
     */
    @GetMapping("/userCourseList")
    public AjaxResult userCourseList(SearChRsp searChRsp) {
        Long userId = SecurityUtils.getUserId();
        HashMap<String, Object> map = new HashMap<>();
        List<TimeUserCard> timeUserCards = timeUserCardService.selectTimeUserCardList(new TimeUserCard().setUserId(userId).setKeyWord(searChRsp.getKeyWord()));
        List<UserCourseCard> userCourseCards = userCourseCardService.selectUserCourseCardList(new UserCourseCard().setUserId(userId).setKeyWord(searChRsp.getKeyWord()));
        map.put("timeUserCards", timeUserCards);
        map.put("userCourseCards", userCourseCards);
        return AjaxResult.success(map);
    }
}
